import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'

Vue.use(Vuex)

Vue.config.productionTip = false

const user = {
  namespaced: true,
  state: {
    token: '唯一标示'
  },
  mutations: {
    changeToken(state) {
      state.token += '111'
    }
  }
}

const setting = {
  state: {
    uname: 'Vuex实例'
  }
}

const store = new Vuex.Store({
  state: {
    count: 10,
    username: '章三',
    list: [2, 1, 4, 6, 8, 10, 5]
  },
  mutations: {
    addCountOne(state) {
      state.count += 1
    },
    addCountNum(state, num) {
      state.count += num
    }
  },
  actions: {
    addAsyncCountOne(context) {
      setTimeout(() => {
        context.commit('addCountOne')
      }, 1000)
    },
    addAsyncCountNum(context, num) {
      setTimeout(() => {
        context.commit('addCountNum', num)
      }, 1000)
    }
  },
  getters: {
    maxList (state) {
      return state.list.filter(item => item >= 5)
    },
    token: state => state.user.token
  },
  modules: {
    user,
    setting
  }
})

new Vue({
  render: h => h(App),
  store
}).$mount('#app')
